package com.jiawa.wiki.controller;

import com.alibaba.fastjson.JSONObject;
import com.jiawa.wiki.req.UserLoginReq;
import com.jiawa.wiki.req.UserQueryReq;
import com.jiawa.wiki.req.UserResetPasswordReq;
import com.jiawa.wiki.req.UserSaveReq;
import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.resp.UserLoginResp;
import com.jiawa.wiki.resp.UserQueryResp;
import com.jiawa.wiki.service.UserService;
import com.jiawa.wiki.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Resource
    private SnowFlake snowFlake;
    @Resource
    private RedisTemplate redisTemplate;
    // 登录
    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req) {
        System.out.println("admin密码：[{}]"+req.getPassword());
        if(!req.getLoginName().equals("admin")) {
            req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        }
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp login = userService.login(req);
        Long token = snowFlake.nextId();
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(login));
        login.setToken(token.toString());
        resp.setContent(login);
        return resp;
    }
    // 新增用户
    @PostMapping("/save")
    public CommonResp add(@Valid @RequestBody UserSaveReq req) {
        if(!req.getLoginName().equals("admin")) {
            req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        }
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        if (ObjectUtils.isEmpty(req.getId())) {
            resp.setMessage("新增用户成功");
        } else {
            resp.setMessage("编辑用户成功");
        }
        return resp;
    }
    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userService.resetPassword(req);
        resp.setMessage("重置密码成功");
        return resp;
    }

    // 登出
    @GetMapping("/logout/{token}")
    public  CommonResp logout(@PathVariable String token) {
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        return resp;
    }

    @GetMapping("/delete/{id}")
    public  CommonResp logout(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        boolean isOK = userService.delete(id);
        if(isOK) {
            resp.setMessage("删除成功");
        } else {
            resp.setMessage("删除失败");
        }
        return resp;
    }
    // 用户列表
    @GetMapping("/list")
    public  CommonResp list(UserQueryReq req) {
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        PageResp<UserQueryResp> list = userService.list(req);
        resp.setContent(list);
        return resp;
    }

}
